package pers.vic.upload.socket.model;

import cn.xuqiudong.common.util.CnToSpellUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.socket.WebSocketSession;
import pers.vic.upload.constant.FileType;
import pers.vic.upload.constant.SessionAttr;
import pers.vic.upload.constant.CommandEnum;

import java.util.Map;

/**
 * 描述: socket初始化指令信息
 * @author Vic.xu
 * @date 2022-06-09 10:37
 */
public class InitCommandInfo {
    /**
     * 当前指令
     */
    private CommandEnum command;

    /**
     * 文件类型
     */
    private FileType type;
    /**
     * 文件长度
     */
    private long total;
    /**
     * 文件名
     */
    private String filename;
    /**
     * 当前片段序号
     */
    private int seq;

    /**
     * 文件唯一标识
     */
    private transient String identity;

    public CommandEnum getCommand() {
        return command;
    }

    public void setCommand(CommandEnum command) {
        this.command = command;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public int getSeq() {
        return seq;
    }

    public void setSeq(int seq) {
        this.seq = seq;
    }

    public FileType getType() {
        return type;
    }

    public void setType(FileType type) {
        this.type = type;
    }

    /**
     * 获取文件标识： ws-upload:userName:文件类型:文件拼音全拼:文件大小
     * @param session
     * @return 文件唯一标识
     */
    public String getIdentity(WebSocketSession session) {
        if (identity == null) {
            Map<String, Object> attributes = session.getAttributes();
            String userName = session.getAttributes().get(SessionAttr.USER_NAME) + "";
            String fullSpell = CnToSpellUtils.getFullSpell(filename);
            identity = "ws-upload:" + userName + ":" + type.name() + ":" + fullSpell + ":" + total;
        }
        return identity;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String s = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(new InitCommandInfo());
        System.out.println(s);
    }
}
